package com.tsinghua.server.common.exception;


import com.tsinghua.server.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler  {


    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public Result handler(RuntimeException e) {
        log.error("运行时异常 ------------- ", e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result serverError(BusinessException e) {
        log.error("业务异常 ------------- ", e.getMessage());
        return Result.fail(e.getCode(), e.getMessage(), null);
    }

}
